<template>
    <div class="main">
        <div class="left-svg">
            <div class="svg-md">
                <svg class="my-svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 685 562">
                    <g mask="url(#unHead)">
                        <g>
                            <image class="svg-anim-pic" x="60" xlink:href="@/assets/imgs/X-bg.jpg" height="100%" />
                        </g>
                    </g>
                </svg>
                <svg style="width:0;height:0;display:block;position:absolute;" aria-hidden="true" xmlns="http://www.w3.org/2000/svg">
                    <defs>
                        <mask id="unHead">
                            <path fill="#fff" d="M77,0H277q42.5,80.492,85,161l1-2L475,20,457,1V0L685,1l-2,2L440,276,609,562H418L334,391l-3,2L192,562H0l1-3L247,276,106,29C98.347,18.032,83.072,12.72,77,0Z" />
                        </mask>
                    </defs>
                </svg>
            </div>
            <img class="tit-pic" src="@/assets/imgs/tit.png" alt="">
        </div>
        <div class="right-form">
            <!--<div class="locale-box">
                <div class="locale-btn" @click="handleSetLanguage"><i class="icon el-icon-setting" /> {{ $t('nav.langSelect') }}</div>
            </div>-->
            <div class="logo-container">
                <img class="c-logo" src="@/assets/imgs/logo.png" alt="">
                <img class="x-logo" src="@/assets/imgs/cha.png" alt="">
                <div class="c-logo c-logo-box">
                    <img class="c-logo-main" src="@/assets/imgs/logo-head.png" alt="">
                    <img class="c-logo-ring" src="@/assets/imgs/logo-ring.png" alt="">
                </div>
            </div>
            <div class="login-container">
                <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="top" hide-required-asterisk>
                    <el-form-item :label="$t('login.account')" prop="userName">
                        <el-input ref="userName" v-model="loginForm.userName" placeholder="" name="userName" type="text" tabindex="1" autocomplete="on" />
                    </el-form-item>

                    <el-form-item :label="$t('login.password')" prop="password">
                        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="" name="password" tabindex="2" autocomplete="on" @keyup.enter.native="inputEnter" />
                        <span class="show-pwd" @click="showPwd"><svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" /></span>
                    </el-form-item>
                    <el-popover v-model="verifyImgShow" placement="top-start" width="326" trigger="click" popper-class="verify-img-popper" @after-enter="verifyImgOpen" :disabled="formPass">
                        <drag-verify-img-chip
                            ref="dragVerify"
                            :width="300"
                            :imgsrc="verifyImg"
                            :is-passing.sync="verifyImgIsPassing"
                            :show-refresh="true"
                            refresh-icon="el-icon-refresh-right"
                            :text="$t('login.verifyText')"
                            :success-tip="$t('login.verifyPass')"
                            :fail-tip="$t('login.verifyFail')"
                            handler-icon="el-icon-d-arrow-right"
                            success-icon="el-icon-circle-check"
                            @refresh="verifyImgRefreash"
                            @passcallback="verifyImgPass"
                        />
                        <el-button ref="loginBtn" slot="reference" class="submit-btn" :loading="loading" type="primary" @click.native.prevent="handleLogin">{{ $t('login.goIn') }}</el-button>
                    </el-popover>
                    <div class="tips">{{ $t('tips.pageHit') }}</div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import dragVerifyImgChip from 'vue-drag-verify-img-chip'
import { mapGetters } from 'vuex'

export default {
    name: 'Login',
    components: {
        dragVerifyImgChip
    },
    data() {
        return {
            verifyImgShow: false,
            verifyImg: require('@/assets/imgs/verifyImgs/1.png'),
            verifyImgIsPassing: false,
            loginForm: {
                userName: '',
                password: ''
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            formPass: true
        }
    },
    computed: {
        ...mapGetters([
            'language'
        ]),
        loginRules() {
            return {
                userName: [{ required: true, trigger: 'blur', message: this.$t('login.accountTips') }],
                password: [{ required: true, trigger: 'blur', message: this.$t('login.passwordTips') }]
            }
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        handleSetLanguage() {
            const lang = this.language === 'en' ? 'zh' : 'en'
            this.$i18n.locale = lang
            this.$store.dispatch('app/setLanguage', lang)
        },
        verifyImgRefreash() {
            console.log('验证码刷新')
            const num = 1// Math.floor(Math.random() * 3) + 1;
            console.log(num)
            this.$refs.dragVerify.reset()
            this.verifyImg = require(`@/assets/imgs/verifyImgs/${num}.png`)
        },
        verifyImgPass() {
            console.log('验证码通过')
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
                this.verifyImgShow = false
                this.$router.push({ path: this.redirect || '/dashboard' })
                // this.loading = false;
            }).catch((err) => {
                console.error(err)
                this.loading = false
                this.verifyImgShow = false
                this.$message.error(this.$t('login.loginFail'))
            })
        },
        verifyImgOpen() {
            // 打开弹窗刷新验证码
            this.$refs.dragVerify.reset()
        },
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        inputEnter(e) {
            this.$refs['loginBtn'].$el.click()
        },
        handleLogin(from) {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    console.log('通过验证，打开图形码')
                    this.formPass = false
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }
    }
}
</script>

<style lang="scss">
.drag-verify-container {
    user-select: none;

    .dv_handler {
        border: 1px solid #ccc;
        box-sizing: border-box;
    }
}

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 100%;

        input {
            background: transparent;
            border-width: 0;
            border-bottom: 1px solid rgba(255, 255, 255, .4);
            -webkit-appearance: none;
            border-radius: 0;
            padding: 12px 30px 12px 10px;
            color: $light_gray;
            height: 47px;
            font-size: 24px;
            caret-color: $cursor;
            font-family: inherit;
            transition: .3s;

            &:focus {
                border-radius: 6px;
                background-color: #434343;
                border-color: rgba(255, 255, 255, 0);
            }

            &:-webkit-autofill {
                box-shadow: 0 0 0 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        background: rgba(0, 0, 0, 0.1);
        color: #454545;
    }

    .el-form-item__label {
        color: #fff;
        font-size: 24px;
        line-height: normal;
        padding-bottom: 4px;
        font-weight: normal;
    }
}
</style>

<style lang="scss" scoped>
.main {
    min-width: 1260px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.left-svg {
    position: absolute;
    top: 50%;
    left: -40px;
    //left: 0;
    z-index: 1;
    transform: translateY(-50%);
    width: 48vw;
    min-width: 700px;
    color: white;
    overflow: hidden;
}
.left-img{
    display: block;
    width: 100%;
}

.svg-mid-md {
    position: absolute;
    left: 0;
    z-index: -1;
}

.svg-btm-md {
    position: absolute;
    left: 0;
    z-index: -2;
}

.svg-anim-pic {
    //transform: translateX(-6rem);
    animation: svgAnim 16s infinite linear alternate-reverse;
}

@keyframes svgAnim {
    0% {
        transform: translateX(0rem);
    }

    100% {
        transform: translateX(-60%);
    }
}

.svg-md {
    position: relative;
}

.my-svg {
    display: block;
}

.tit-pic {
    position: absolute;
    top: 50%;
    left: 20%;
    width: 67%;
    user-select: none;
    pointer-events: none;
}

.tit-pic {
    transform: translate3d(0, -50%, 0);
}

.head-pic-md {
    margin-bottom: 8.31rem;
}

.icon-pic,
.logo-pic {
    width: 6.81rem;
}

.cha-pic {
    width: 2.37rem;
    margin: 0 3.16rem;
}

/*左边X结束*/
.right-form {
    position: relative;
    min-width: 460px;
    margin-left: auto;
    margin-right: 8vw;
    z-index: 2;
    .locale-box{
        position: absolute;
        top: -60px;
        right: 0;
        .locale-btn{
            display: flex;
            align-items: center;
            font-size: 20px;
            line-height: 25px;
            color: #fff;
            cursor: pointer;
            .icon{
                display: block;
                margin-right: 8px;
            }
        }
    }
}

.logo-container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 130px;

    .c-logo {
        display: block;
        width: 108px;
        height: 108px;
    }

    .c-logo-box {
        position: relative;

        .c-logo-main {
            position: relative;
            display: block;
            width: 100%;
            height: 100%;
            z-index: 1;
        }

        .c-logo-ring {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 2;
            animation: rotateRound 16s infinite linear;
        }
    }

    .x-logo {
        margin: 0 50px;
        width: 38px;
    }
}

$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    width: 100%;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 100%;
        overflow: hidden;
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .tips {
        font-size: 24px;
        color: #fff;
        white-space: nowrap;
        opacity: .3;
    }

    .submit-btn {
        padding: 0;
        margin-bottom: 20px;
        width: 100%;
        height: 52px;
        line-height: 52px;
        font-size: 30px;
        font-family: inherit;
        background-color: #1D38FF;
        border-color: #1D38FF;
        box-sizing: border-box;
    }
}
</style>
